let storage = bui.storage();
let translate
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            let language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }


            let uiLoading = bui.loading({
                appendTo:"#rigisterLoading",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"注册中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });

            let params = module.props;
            router.$("#registerFh").click(function () {
                router.replace({ url: "pages/login/login.html", param:{tzSign: params.tzSign}});
            })

            let accountStatus = false
            let passwordStatus = false
            let passwordConfStatus = false
            let emailStatus = false
            let org = storage.get("org",0)
            router.$("#cs1").html(org.description)

            // 密码显示或者隐藏
            let account = bui.input({
                id: ".user-input",
                showIcon: false,
                onInput: function(e) {
                    if (!e.target.value) {
                        accountStatus = false
                        router.$("#accountTs1").html("账号不可为空");
                        return false;
                    } else {
                        accountStatus = true
                        router.$("#accountTs1").html("");
                    }
                    return true;
                }
            })
            // 密码显示或者隐藏
            let password = bui.input({
                id: "#passwordInput1",
                iconClass: ".icon-eye",
                onInput: function(e) {
                    e.target.value = e.target.value.trim()
                    // 注册的时候校验只能4-18位密码
                    let rule = /^[a-zA-Z0-9_-]{4,18}$/;
                    if (!rule.test(e.target.value)) {
                        passwordStatus = false
                        router.$("#passwordTs1").html("密码只能4-18位")
                        return false;
                    } else {
                        passwordStatus = true
                        router.$("#passwordTs1").html("")
                    }
                    return true;
                },
                callback: function(e) {
                    //切换类型
                    this.toggleType();
                    //
                    router.$(e.target).toggleClass("active")
                }
            })
            bui.input({
                id: "#passwordConfirm",
                iconClass: ".icon-eye",
                onInput: function(e){
                    e.target.value = e.target.value.trim()
                },
                onBlur: function(e) {
                    let val = password.value();
                    if (val !== e.target.value) {
                        passwordConfStatus = false
                        router.$("#passwordConTs").html("两次密码需要保持一致")
                    } else {
                        passwordConfStatus = true
                        router.$("#passwordConTs").html("")
                    }
                    return true;
                },
                callback: function(e) {
                    //切换类型
                    this.toggleType();
                    //
                    router.$(e.target).toggleClass("active")
                }
            })
            let email = bui.input({
                id: "#mailInput",
                showIcon: false,
                onInput: function(e) {
                    let rule = /^([a-zA-Z\d][\w-]{2,})@(\w{2,})\.([a-z]{2,})(\.[a-z]{2,})?$/
                    if (e.target.value) {
                        if (!rule.test(e.target.value)) {
                            emailStatus = false
                            router.$("#mailTs").html("邮箱格式不对")
                            return false;
                        } else {
                            emailStatus = true
                            router.$("#mailTs").html("")
                        }
                    } else {
                        emailStatus = false
                        router.$("#mailTs").html("邮箱不可为空")
                        return false;
                    }

                    return true;
                }
            })

            router.$("#regis").click(function () {
                if (accountStatus && passwordStatus && passwordConfStatus && emailStatus) {
                    uiLoading.show()
                    // bui.btn("#regis").submit(function(loading) {
                        //查询菜单
                        bui.ajax({
                            url: global.api.register,
                            data: {userName: account.value(), password: password.value(), nickName: router.$("#nickName").val(), email: email.value(), country: org.parent, city: org.id},
                            method: "POST"
                        }).then(function(res){
                            if (res.code == '200') {
                                bui.confirm('注册成功', function(e) {
                                    //this 是指点击的按钮
                                    let text = router.$(e.target).text();

                                    if (text == "确定") {
                                        storage.set("token", res.token)
                                        storage.set("user", res)

                                        storage.set("tzPage", 0)
                                        router.replace({ url: "pages/mainIndex/mainIndex.html"});
                                    }
                                    this.close()
                                });
                            } else {
                                bui.alert(res.msg)
                            }
                            uiLoading.stop()
                            // loading.stop()
                        })
                    // })
                } else {
                    if (!accountStatus) {
                        router.$("#accountTs1").html("账号不可为空");
                    }
                    if (!passwordStatus) {
                        router.$("#passwordTs1").html("密码只能4-18位")
                    }
                    if (!passwordConfStatus) {
                        router.$("#passwordConTs").html("两次密码需要一致")
                    }
                    if (!emailStatus) {
                        router.$("#mailTs").html("邮箱不可为空")
                    }
                    bui.hint("输入数据不符合规则，请检查后重试")
                }
            })
        }
    }
    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})
